{% extends 'stocks/base.html' %}

{% block title %}{{ stock.symbol }} - 财务指标{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>
        <i class="bi bi-calculator"></i> {{ stock.symbol }} - {{ stock.name }} 财务指标
    </h2>
    <div class="btn-group">
        <a href="{% url 'stock_detail' stock.symbol %}" class="btn btn-outline-primary">
            <i class="bi bi-building"></i> 公司详情
        </a>
        <a href="{% url 'daily_data' stock.symbol %}" class="btn btn-outline-success">
            <i class="bi bi-bar-chart"></i> 交易数据
        </a>
        <a href="{% url 'stock_list' %}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> 返回列表
        </a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h5><i class="bi bi-graph-up"></i> 财务分析指标</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-sm table-hover">
                <thead class="table-light">
                    <tr>
                        <th>报告期</th>
                        <th>稀释EPS</th>
                        <th>加权EPS</th>
                        <th>ROE(%)</th>
                        <th>销售净利率(%)</th>
                        <th>毛利率(%)</th>
                        <th>资产负债率(%)</th>
                        <th>流动比率</th>
                        <th>速动比率</th>
                        <th>总资产(万元)</th>
                        <th>营收增长率(%)</th>
                        <th>净利润增长率(%)</th>
                    </tr>
                </thead>
                <tbody>
                    {% for data in page_obj %}
                    <tr>
                        <td>{{ data.date }}</td>
                        <td>{{ data.diluted_eps|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.weighted_eps|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.roe|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.net_profit_margin_on_sales|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.gross_profit_margin|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.asset_liability_ratio|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.current_ratio|default:"-"|floatformat:2 }}</td>
                        <td>{{ data.quick_ratio|default:"-"|floatformat:2 }}</td>
                        <td>
                            {% if data.total_assets %}
                                {{ data.total_assets|floatformat:2 }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td class="{% if data.main_business_income_growth_rate and data.main_business_income_growth_rate > 0 %}positive-change{% elif data.main_business_income_growth_rate and data.main_business_income_growth_rate < 0 %}negative-change{% endif %}">
                            {{ data.main_business_income_growth_rate|default:"-"|floatformat:2 }}
                        </td>
                        <td class="{% if data.net_profit_growth_rate and data.net_profit_growth_rate > 0 %}positive-change{% elif data.net_profit_growth_rate and data.net_profit_growth_rate < 0 %}negative-change{% endif %}">
                            {{ data.net_profit_growth_rate|default:"-"|floatformat:2 }}
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="12" class="text-center text-muted py-4">
                            <i class="bi bi-calculator display-4"></i>
                            <p class="mt-2">暂无财务数据</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if page_obj.paginator.num_pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?page=1">首页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
                </li>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                    </li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">末页</a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}

        <div class="text-muted text-center mt-3">
            显示 {{ page_obj.start_index }} - {{ page_obj.end_index }} 条，共 {{ page_obj.paginator.count }} 条记录
        </div>
    </div>
</div>

<div class="row mt-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h6><i class="bi bi-info-circle"></i> 财务指标说明</h6>
            </div>
            <div class="card-body">
                <ul class="list-unstyled small">
                    <li><strong>EPS:</strong> 每股收益</li>
                    <li><strong>ROE:</strong> 净资产收益率</li>
                    <li><strong>销售净利率:</strong> 净利润/营业收入</li>
                    <li><strong>毛利率:</strong> (营业收入-营业成本)/营业收入</li>
                    <li><strong>资产负债率:</strong> 总负债/总资产</li>
                    <li><strong>流动比率:</strong> 流动资产/流动负债</li>
                    <li><strong>速动比率:</strong> (流动资产-存货)/流动负债</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h6><i class="bi bi-database"></i> 数据来源</h6>
            </div>
            <div class="card-body">
                <p class="small">以上财务数据来源于MCP stock_db数据库的stock_financial_analysis_indicator表，包含公司的各项财务分析指标。</p>
            </div>
        </div>
    </div>
</div>
{% endblock %}
